package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.entity.User;
import com.example.demo.exception.BizException;
import com.example.demo.exception.BizSunException;
import com.example.demo.feign.FeignClientServer;
import com.example.demo.kafka.Product;
import com.example.demo.service.UserService;
import com.example.demo.util.EmptyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController

@Api(tags = "用户信息")
public class UserController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private FeignClientServer feignClient;

    @Autowired
    private Product product;


    /**
     * 查询用户列表
     * @return
     */
    @ApiOperation(value = "查询用户列表", notes = "数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",value = "用户名称",required = false, paramType = "query"),
            @ApiImplicitParam(name="age",value = "年龄",required = false, paramType = "query")
    })
    @RequestMapping(value = "getUserList",method = RequestMethod.GET)
    public List<User> getUserList(){
        return userService.getUserList();
    }

    /**
     * 新增用户信息
     * @param user
     * @return
     */
    @ApiOperation(value = "新增用户", notes = "数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",value = "用户名称",required = true, paramType = "query"),
            @ApiImplicitParam(name="age",value = "年龄",required = true, paramType = "query")
    })
    @RequestMapping(value = "addUser",method = RequestMethod.GET)
    public List<User> addUser(User user){
        if(EmptyUtil.isEmpty(user) || null == user.getAge()){
            throw new BizException();
        }
        try {
            userService.addUser(user);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return userService.getUserList();
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    //@RequestMapping(value = "updateUser",method = RequestMethod.GET)
    @RequestMapping(value = "updateUser",method = RequestMethod.POST)
    public List<User> updateUser(@RequestBody User user){
        if(null == user || null == user.getId()){
            return null;
        }
        try {
            User queryUser = new User();
            queryUser = userService.queryUser(user);
            if(queryUser != null ) {
                userService.updateUser(user);
            }else{
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return userService.getUserList();
    }

    /**
     * 通过id查询用户信息
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id查询用户", notes = "数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "用户id",required = true, paramType = "query")
    })
    @RequestMapping(value = "queryUserById",method = RequestMethod.GET)
    public User queryUserById(Integer id){
        if(1==1){
            //throw new BizSunException();
        }
        if(EmptyUtil.isEmpty(id)){
            throw new BizException();
        }
        return userService.queryUserById(id);
    }

    @ApiOperation(value = "kafka发送用户信息", notes = "数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "用户id",required = true, paramType = "query")
    })
    @RequestMapping(value = "kafkaSendUser",method = RequestMethod.GET)
    public User kafkaSendUser(Integer id){
        log.info("由于kafka集群比较占用服务器内存，现已暂停服务，使用时请按文档重启");
        User user = new User();
        user.setName("小王");
        user.setAge(30);
        user.setId(1);
        for(int i=0; i<1; i++) {
            product.send("user",JSON.toJSONString(user));
        }
        return user;
    }
}
